package com.vessel.decoration.mvp.presenter.mine.manage;

import android.app.Application;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.vessel.decoration.app.constant.Constants;
import com.vessel.decoration.app.data.entity.ArtisanInfoResponse;
import com.vessel.decoration.app.data.entity.TypeListResponse;
import com.vessel.decoration.mvp.contract.mine.manage.WorkerContract;
import com.vessel.decoration.mvp.ui.adapter.WorkerSkillAdapter;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * @author vesselzhang
 * @date 2017/12/5
 */

@ActivityScope
public class WorkerPresenter extends BasePresenter<WorkerContract.Model, WorkerContract.View>
        implements AdapterView.OnItemClickListener {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private AppManager mAppManager;

    private WorkerSkillAdapter mAdapter;
    private List<TypeListResponse.TypesBean> typeList;

    @Inject
    public WorkerPresenter(WorkerContract.Model model, WorkerContract.View rootView
            , RxErrorHandler handler, Application application, AppManager appManager
            , WorkerSkillAdapter adapter) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mAppManager = appManager;

        this.mAdapter = adapter;
    }

    public void getInfo() {
        mModel.authorityArtisanInfo()
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(
                        new ErrorHandleSubscriber<ArtisanInfoResponse>(mErrorHandler) {
                            @Override
                            public void onNext(ArtisanInfoResponse artisanInfoResponse) {
                                if (artisanInfoResponse == null) {
                                    return;
                                }
                                mRootView.setWorkerInfo(artisanInfoResponse);
                                queryTypeList(artisanInfoResponse.getTypeId());
                                if (artisanInfoResponse.getSkills() != null) {
                                    mAdapter.setList(artisanInfoResponse.getSkills());
                                }
                                mAdapter.notifyDataSetChanged();
                            }
                        }
                );
    }

    public void queryTypeList(int typeId) {
        mModel.queryTypeList(0)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(
                        new ErrorHandleSubscriber<TypeListResponse>(mErrorHandler) {
                            @Override
                            public void onNext(TypeListResponse typeListResponse) {
                                typeList = typeListResponse.getTypes();
                                for (TypeListResponse.TypesBean type : typeList) {
                                    if (type.getTypeId() == typeId) {
                                        mRootView.showType(type.getTypeName());
                                        break;
                                    }
                                }
                            }
                        }
                );
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ARouter.getInstance().build("/app/worker/skill")
                .withSerializable(Constants.TYPE, Constants.TYPE_UPDATE)
                .withSerializable("data", (ArtisanInfoResponse.SkillsBean) adapterView.getAdapter().getItem(i))
                .navigation();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mApplication = null;
    }
}